// Jocara game project

#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED

#include "precompiled_header.h"
#include "miscellaneous.h"

GameModes Game();

enum TileType {
  kTileTypeFree,
  kTileTypeOccupied
};

enum TileTexture {
  kTileGround,
  kTileGrass,
  kTileWater
};

enum Status {
  kIdle,
  kProcessing,
  kReady
};

struct Tile {
  TileType type;
  TileTexture texture;
  int8_t level;
};

struct Vector2D {
  int x;
  int y;
};

enum AlphaMode {
  kAlphaDissolve,
  kAlphaFade,
  kAlphaDistort
};

enum FadeType {
  kFadeCorner,
  kFadeLine,
  kFadeL,
  kFadeU,
  kFadeO
};

struct AlphaNeighborhood {
  Tile* tile;
  bool plus[4];
  bool ex[4];
  int8_t count_plus;
  int8_t count_ex;
};

inline void AddVector(const Vector2D* src, Vector2D* dst) {
  dst->x += src->x;
  dst->y += src->y;
}

void GenerateMap(const unsigned int seed);
//Tile* MapData(const unsigned int x, const unsigned int y);

class LocalMap {
public:
  LocalMap();
  ~LocalMap();
  void update(const int x, const int y);
  void update_sync(const int x_, const int y_);
  inline Status status() {return status_;}
  inline SDL_Surface* image() {return image_;}
  inline void reset() {status_ = kIdle;}
protected:
  void apply_fades(SDL_Surface* surf, const int x, const int y);
  SDL_Surface* rotated_fade(AlphaMode alpha_mode, FadeType fade, uint8_t t);
  void tex_blit(SDL_Surface* surf, TileTexture tex);
  void tex_blit_alpha(SDL_Surface* surf, TileTexture tex);
  Tile* local_map_data_;
  SDL_Surface* image_;
  Status status_;
  SDL_Thread* thread_;
};

void swap_buffers();
Vector2D AbsoluteTile(const int x, const int y);
int BackgroundLoader(void* data);
int BackgroundLoaderArea(void* data);
bool CompareTileLevels(AlphaNeighborhood* a, AlphaNeighborhood* b);

#endif // GAME_H_INCLUDED
